package com.webserver.http;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 响应对象
 * 该类的每一个实例用于表示HTTP协议要求的响应
 * 每个响应由三部分组成：
 * 状态行，响应头，响应正文
 */
public class HttpServletResponse {
    private Socket socket;
    //状态行相关信息
    private int statusCode = 200;
    private String statusReason = "OK";
    //响应头相关信息
    private Map<String , String> headers = new HashMap<>();
    //响应正文相关信息
    private File contentFile;

    public HttpServletResponse(Socket socket){
        this.socket = socket;
    }

    public void response() throws IOException {
        //3.1发送状态行
        sendStatusLine();
        //3.2发送响应头
        sendHeaders();
        //3.2发送响应正文
        sendContent();

    }

    private void sendStatusLine() throws IOException {
        println("HTTP/1.1"+" "+statusCode+" "+statusReason);
    }

    private void sendHeaders() throws IOException {
//        println("Content-Type: text/html");
//        println("Content-length: "+contentFile.length());
        Set<Map.Entry<String,String>> entrySet = headers.entrySet();
        for (Map.Entry<String,String> e : entrySet){
            String key = e.getKey();
            String value = e.getValue();
            println(key+": "+value);
        }
        println("");
    }

    private void sendContent() throws IOException {
        OutputStream out = socket.getOutputStream();
        int len;
        byte[] data = new byte[1024*10];
        FileInputStream fis = new FileInputStream(contentFile);
        while ((len = fis.read(data))!=-1){
            out.write(data,0,len);
        }
        System.out.println("响应发送完毕!!!!");
    }


    private void println(String line) throws IOException {
        OutputStream out = socket.getOutputStream();
        byte[] data = line.getBytes(StandardCharsets.ISO_8859_1);
        out.write(data);
        out.write(13);
        out.write(10);
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    public File getContentFile() {
        return contentFile;
    }

    public void setContentFile(File contentFile) {
        this.contentFile = contentFile;
        String contentType = null;
        try {
            contentType = Files.probeContentType(contentFile.toPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        /*
            当我们无法根据正文文件确定类型时，则不发送Content-Type
            在HTTP协议中当响应包含正文，但是不包含Content-Type时，则表示让浏览器
            自行判断响应正文内容
         */
        if (contentType!=null){
            addHeaders("Content-Type",contentType);
        }
        addHeaders("Content-Length",contentFile.length()+"");//long值变为String类型
    }

    /**
     * 添加一个要发送的响应头
     * @param name
     * @param value
     */
    public void addHeaders(String name,String value){
        //将响应头的名字和值已key-value形式存入headers这个Map中
        headers.put(name, value);
    }
}
